/**
 * 
 */

package com.wutianyi.arithmetic;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;

/**
 * @author wutianyi
 * 
 */
public class MyDigitalSignature {

	public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
		String message = "wutianyi_digital_signature";
		
		byte[] plainText = message.getBytes("utf8");
		
		KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
		generator.initialize(1024);
		
		KeyPair key = generator.generateKeyPair();
		
		Signature sig= Signature.getInstance("MD5WithRSA");
		sig.initSign(key.getPrivate());
		sig.update(plainText);
		byte[] signature = sig.sign();
		System.out.println(new String(signature, "utf8"));
		sig.initVerify(key.getPublic());
		sig.update(plainText);
		if(sig.verify(signature)) {
			System.out.println("signature verified");
		}
		
		
		
	}
}
